package com.example.module_home.house

import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_common.view.view.BaseMvvmActivity
import com.example.module_home.R
import com.example.module_home.adapter.HouseAdapter
import com.example.module_home.adapter.HouseAreaAdapter
import com.example.module_home.databinding.ActivityHouseBinding
import com.example.module_home.entity.MyHouseEntity
import com.example.module_home.vm.HouseViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/HouseActivity")
class HouseActivity : BaseMvvmActivity<ActivityHouseBinding>() {
    lateinit var houseAreaAdapter:HouseAreaAdapter
    private val houseViewModel :HouseViewModel by viewModels()


    override fun getLayoutId(): Int =R.layout.activity_house

    override fun initViews() {
        binding.moneyManage.setOnClickListener {
            ARouter.getInstance().build("/app/TenantHouseActivity").navigation()
        }

        houseAreaAdapter=HouseAreaAdapter(houseViewModel)
        binding.houseRv.adapter=houseAreaAdapter
        houseViewModel.getHouse()
//        houseViewModel.mSuccessLiveData.observe(this){
//            houseAreaAdapter.data.add()
//            houseAreaAdapter.notifyDataSetChanged()
//        }
//        houseViewModel.mErrorLiveData.observe(this){errorMsg->
//            Toast.makeText(this,errorMsg, Toast.LENGTH_SHORT).show()
//        }

        binding.rgHouse.setOnCheckedChangeListener { group, checkedId ->
            houseAreaAdapter.data.clear()
            if (checkedId==R.id.aaa){
                houseViewModel.getQueryHouseId(1)
            }else if (checkedId==R.id.bbb){
                houseViewModel.getQueryHouseId(2)
            }else if (checkedId==R.id.ccc){
                houseViewModel.getQueryHouseId(3)
            }else if (checkedId==R.id.ddd){
                houseViewModel.getQueryHouseId(4)
            }
            houseAreaAdapter.notifyDataSetChanged()
        }

        binding.houseABC.setOnCheckedChangeListener { group, checkedId ->
            houseAreaAdapter.data.clear()
            if (checkedId==R.id.house_a){
              houseViewModel.mSuccessLiveData.observe(this){
                  val map = it.data.groupBy { it.area }
                  val list = map.keys.sorted()
                  val houseBean= mutableListOf<MyHouseEntity>()
                  var index=1
                  for (key in list){
                      houseBean.add(MyHouseEntity(index,key,map[key]!!))
                      index++
                  }
                  houseAreaAdapter.data.clear()
                  houseAreaAdapter.addData(houseBean.filter { it.dong==1 })
                  houseAreaAdapter.notifyDataSetChanged()
              }
            }else if (checkedId==R.id.house_b){
                houseViewModel.mSuccessLiveData.observe(this){
                    val map = it.data.groupBy { it.area }
                    val list = map.keys.sorted()
                    val houseBean= mutableListOf<MyHouseEntity>()
                    var index=1
                    for (key in list){
                        houseBean.add(MyHouseEntity(index,key,map[key]!!))
                        index++
                    }
                    houseAreaAdapter.data.clear()
                    houseAreaAdapter.addData(houseBean.filter { it.dong==2 })
                    houseAreaAdapter.notifyDataSetChanged()
                }
            }else if (checkedId==R.id.house_c){
                houseViewModel.mSuccessLiveData.observe(this){
                    val map = it.data.groupBy { it.area }
                    val list = map.keys.sorted()
                    val houseBean= mutableListOf<MyHouseEntity>()
                    var index=1
                    for (key in list){
                        houseBean.add(MyHouseEntity(index,key,map[key]!!))
                        index++
                    }
                    houseAreaAdapter.data.clear()
                    houseAreaAdapter.addData(houseBean.filter { it.dong==3 })
                    houseAreaAdapter.notifyDataSetChanged()
                }
            }
            houseAreaAdapter.notifyDataSetChanged()
        }


        houseViewModel.mSuccessLiveData.observe(this){
           houseAreaAdapter.data.clear()
            val map = it.data.groupBy { it.area }//根据面积分组
            val list = map.keys.sorted()//根据面积排序
            val houseBean= mutableListOf<MyHouseEntity>()
            var index=1
            for (key in list){
                houseBean.add(MyHouseEntity(index,key,map[key]!!))
                index++
            }
            houseAreaAdapter.addData(houseBean.filter { it.dong==1 })
            houseAreaAdapter.notifyDataSetChanged()
        }
    }

    override fun getActivityState(state: Bundle) {

    }

    override fun saveActivityState(outState: Bundle) {

    }

}